package cn.zcib.film.service.impl;

import cn.zcib.film.anno.Record;
import cn.zcib.film.bean.Subject;
import cn.zcib.film.bean.vo.SellVO;
import cn.zcib.film.bean.vo.SubjectVO;
import cn.zcib.film.mapper.SubjectMapper;
import cn.zcib.film.service.SubjectService;
import cn.zcib.film.util.UUIDUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Slf4j
public class SubjectServiceImpl implements SubjectService {
    @Autowired
    private SubjectMapper subjectMapper;
    @Override
    public int addSub(Subject subject) {
        return subjectMapper.addSubject(subject);
    }

    @Override
    public int updateSub(Subject subject) {
        return subjectMapper.updateSubjectById(subject);
    }

    @Override
    public int delSubById(Integer sId) {
        return subjectMapper.deleteSubjectById(sId);
    }

    @Override
    public List<Subject> findSubs(SubjectVO vo) {
        return subjectMapper.getSubjects(vo);
    }

    @Override
    public int getSubCountByTypeId(Integer typeId) {
        return subjectMapper.getSubCountByTypeId(typeId);
    }

    @Override
    public int getSubCountByTypeIds(List<Integer> tIds) {
        return subjectMapper.getSubCountByTypeIds(tIds);
    }

    @Override
    public int countSubjects() {
        return subjectMapper.countSubjects();
    }

    @Override
    public int groupDelSubject(List<Integer> sIds) {
        return subjectMapper.groupDelSubject(sIds);
    }

    @Override
    public Map<String,Object> picUpload(MultipartFile file) {
        //返回信息
        HashMap<String, Object> map = new HashMap<>();
        //判断是否选中文件
        if (!file.isEmpty()){
            //获取文件上传地址
            System.out.println("获取文件上传地址");
            Resource resource = new ClassPathResource("");
            String path= null;
            try {
                path = resource.getFile().getAbsolutePath() + "/static/mainpic";
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("path = " + path);
            //获取源文件名称
            String oldName = file.getOriginalFilename();
            //获取文件后缀名
            String extension = FilenameUtils.getExtension(oldName);
            //重命名文件名称
            String newFileName = UUIDUtils.randomUUID()+"."+extension;
            //使用日期文件夹管理
            String datePath = new SimpleDateFormat("yyyyMMdd").format(new Date());
            //组装最终的文件名
            String finalName = datePath + "/" + newFileName;
            //创建文件对象
            File destFile = new File(path,finalName);
            //判断文件夹是否存在，不存在则创建
            if(!destFile.getParentFile().exists()){
                destFile.getParentFile().mkdirs();
            }
            //保存
            try{
                file.transferTo(destFile);
                System.out.println("============上传完毕==============");
                //保存响应数据
                map.put("code",0);
                map.put("msg","");
                HashMap<String, Object> dataMap = new HashMap<>();
                dataMap.put("src","/static/mainpic/"+finalName);//文件上传成功后的回显地址
                map.put("data",dataMap);
//                map.put("imageName",finalName);//图片名称，目的是给img
            }catch (IOException e){
                e.printStackTrace();
            }
        }
        return map;
    }

    @Override
    public Map<String ,Integer> getSell() {
        List<SellVO> sell = subjectMapper.getSell();
        HashMap<String, Integer> map = new HashMap<>();
        for (SellVO vo : sell) {
            //算出总票房
            int totalMoney = vo.getMoney() * vo.getCount();
            map.put(vo.getsName(), totalMoney);
        }
        //打印电影名：总票房
//        for (Map.Entry<String, Integer> entry : map.entrySet()) {
//            log.info(entry.getKey()+":"+entry.getValue());
//        }
        return map;
    }

    @Override
    public List<Subject> getTopStar() {
        return subjectMapper.getTopStar();
    }


}
